Skip to content
This repository was archived by the owner on Aug 14, 2026. It is now read-only.

Secure Source of Randomness - #436

Closed
pixeebot[bot] wants to merge 2 commits into
mainfrom
pixeebot/drip-2024-02-20-pixee-python/secure-random
Closed

pixeebot[bot] wants to merge 2 commits into
mainfrom
pixeebot/drip-2024-02-20-pixee-python/secure-random

Conversation

@pixeebot

@pixeebot pixeebot Bot commented Feb 20, 2024

Copy link
Copy Markdown
Contributor

This codemod replaces all instances of functions in the random module (e.g. random.random() with their, much more secure, equivalents from the secrets module (e.g. secrets.SystemRandom().random()).

There is significant algorithmic complexity in getting computers to generate genuinely unguessable random bits. The random.random() function uses a method of pseudo-random number generation that unfortunately emits fairly predictable numbers.

If the numbers it emits are predictable, then it's obviously not safe to use in cryptographic operations, file name creation, token construction, password generation, and anything else that's related to security. In fact, it may affect security even if it's not directly obvious.

Switching to a more secure version is simple and the changes look something like this:

- import random
+ import secrets
  ...
- random.random()
+ secrets.SystemRandom().random()
More reading

I have additional improvements ready for this repo! If you want to see them, leave the comment:

@pixeebot next

... and I will open a new PR right away!

Powered by: pixeebot (codemod ID: pixee:python/secure-random)

@dwanderson-intel

Copy link
Copy Markdown
Contributor

aesthetic change only: moving the import lines back into stdlib, which is i think where they belong (not a 3rd party install)

@dwanderson-intel

Copy link
Copy Markdown
Contributor

non-crypto uses of random

@dwanderson-intel
dwanderson-intel deleted the pixeebot/drip-2024-02-20-pixee-python/secure-random branch February 20, 2024 22:03
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant